const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {scrollheight:wx.getSystemInfoSync().windowHeight-100,
    dialogOpen:false,
    commentDialogOpen:false,
    sendContent:"", // 记录的笔记
    sendCommentContent:"",// 留言内容
    knowledgeDetail:{}, // 知识详情
    swiperList: [], // 轮播图
    knowledgeId:null, // 知识id
    commentList:[],// 评论列表
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.scrollheight)
      this.setData({
        knowledgeId:options.id 
      })
      // 获取知识详情
      this.getKnowLedgeDetail(options.id)

      
      // 记录学习
      this.startLearningKnowledge(options.id);

      // 获取知识评论
      this.getKnowLedgeCommentByKnowledgeId(options.id)
     
  },

  /**
   * 获取知识评论
   */
  getKnowLedgeCommentByKnowledgeId(knowledgeId){
   
    let that = this;
    app.util.request({
      url: "knowledge/getKnowLedgeCommentByKnowledgeId/"+knowledgeId,
      method: "get",
      success: function (res) {
        if(res.data.code == 200){
          that.setData({
            commentList:res.data.data
          })
        }
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 开始学习
   */
  startLearningKnowledge(knowledgeId){

    let params = {};
    params.knowledgeId = knowledgeId;
    params.userId = app.globalData.userInfo.id
    app.util.request({
      url: "knowledge/startLearningKnowledge",
      method: "post",
      data:params,
      success: function (res) {
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },
  /**
   * 获取知识详情
   * knowledge/getKnowLedgeDetail/{knowledgeId}
   */
  getKnowLedgeDetail(knowledgeId){
    let that = this;
    app.util.request({
      url: "knowledge/getKnowLedgeDetail/"+knowledgeId+"?userId="+app.globalData.userInfo.id,
      method: "get",
      success: function (res) {
        // 轮播图list
        res.data.data.cover = app.imageUrl + res.data.data.cover
        let swiperList = [];
        swiperList.push(res.data.data)
        that.setData({
          swiperList: swiperList,
          knowledgeDetail:res.data.data
        })
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 收藏按钮
   */
  toCollect(){
    let detail = this.data.knowledgeDetail;
    if(detail.isCollect){
      // 取消收藏
      this.cancelCollect()
    }else{
      // 收藏
      this.doCollect()
    }
    
  },

  /**
   * 收藏
   */
  doCollect(){
    
    let that = this;
    let params = {}
    params.knowledgeId = this.data.knowledgeId
    params.userId = app.globalData.userInfo.id
    app.util.request({
      url: "knowledge/addKnowledgeCollect",
      method: "post",
      data:params,
      success: function (res) {
        if(res.data.code == 200){
          wx.showToast({
            title: '收藏成功',
          })
          let detail = that.data.knowledgeDetail;
          detail.isCollect = detail.isCollect?false:true
          that.setData({
            knowledgeDetail:detail
          })
          
        }else{
          wx.showToast({
            title: '系统异常',
            icon:'none'
          })
        }
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
    * 取消收藏
    */
   cancelCollect(){ 
    let queryParam = "?knowledgeId="+this.data.knowledgeId
    queryParam += "&userId="+app.globalData.userInfo.id
    let that = this;
    app.util.request({
      url: "knowledge/cancelKnowledgeCollect"+queryParam,
      method: "post",
      showLoading:true,
      success: function (res) {

        if(res.data.code == 200){
          wx.showToast({
            title: '取消成功',
          })
          let detail = that.data.knowledgeDetail;
          detail.isCollect = detail.isCollect?false:true
          that.setData({
            knowledgeDetail:detail
          })
        }
        
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 打开记笔记窗口
   */
  openNotesDialog(){
    let dialogOpen = this.data.dialogOpen?false:true;
    this.setData({
      dialogOpen:dialogOpen
    })
  },

  /**
   * 打开留言
   */
  openCommentDialog(){
    let commentDialogOpen = this.data.commentDialogOpen?false:true;
    this.setData({
      commentDialogOpen:commentDialogOpen
    })
  },

   /**
   * 跳转到考试页面
   */
  toexam(){
    
    wx.navigateTo({
      url: '/pages/toexam/toexam?knowledgeId='+this.data.knowledgeId,
    })
  },

  

  /**
   * 绑定输入的直
   */
  bindTextInput(e){
    this.setData({
      sendContent : e.detail.value
    })
  },
  bindCommentInput(e){
    this.setData({
      sendCommentContent : e.detail.value
    })
  },

  /**
   * 取消按钮
   */
  cancel(){
    this.setData({
      dialogOpen:false
    })
  },
  commentCancel(){
    this.setData({
      commentDialogOpen:false
    })
  },

  /**
   * 保存按钮
   */
  save(){ 

    let params = {};
    params.knowledgeId = this.data.knowledgeId
    params.userId = app.globalData.userInfo.id
    params.content = this.data.sendContent
    console.log(this.data.sendContent)
    let that = this;
    app.util.request({
      url: "note/addKnowledgeNote",
      method: "post",
      data:params,
      success: function (res) {
        // 轮播图list
       if(res.data.code == 200){
          wx.showToast({
            title: '记录成功',
            icon:"success"
          })
          that.setData({
            dialogOpen:false,
            sendContent:""
          })
       }
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })



    
   

  },

  commentSave(){
    let params = {};
    params.knowledgeId = this.data.knowledgeId
    params.userId = app.globalData.userInfo.id
    params.content = this.data.sendCommentContent
    params.pid = 0
    let that = this;
    app.util.request({
      url: "knowledge/addCommentKnowledge",
      method: "post",
      data:params,
      success: function (res) {
       
       if(res.data.code == 200){
          wx.showToast({
            title: '留言成功',
            icon:"success"
          })
          that.getKnowLedgeCommentByKnowledgeId(that.data.knowledgeId)
          that.setData({
            commentDialogOpen:false,
            sendCommentContent:""
          })
       }
        
      },
      fail:function(){
        wx.showToast({
          title: '网路异常',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})